package com.lcredu.spring;

import com.lcredu.bean.*;
import com.lcredu.component.UserController;
import com.lcredu.component.UserService;
import com.lcredu.component.controller.MyController;
import com.lcredu.controller.OrderController;
import com.lcredu.depinjection.PhoneService;
import com.lcredu.factory.MyStaticFactory;
import com.lcredu.service.MemberServiceImpl;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author 黎长容
 * @date 2025-07-12 14:32
 */
public class BeanTest02 {

    /**
     * bean--泛型依赖注入
     */
    @Test
    public void setByT() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans07.xml");
        PhoneService phoneService = ioc.getBean("phoneService", PhoneService.class);
        phoneService.save();
        System.out.println("ok");
    }


    /**
     * bean--基于注解(Autowired/Resource)进行自动装配
     */
    @Test
    public void setBeanByAutowired() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans06.xml");
        UserController controller = ioc.getBean(UserController.class);
        UserService userService = ioc.getBean("userService", UserService.class);
        System.out.println("userService=" + userService);
        controller.sayOk();
        System.out.println("ok");
    }


    /**
     * bean--基于注解自动装配
     */
    @Test
    public void getBean21() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans05.xml");
        UserController controller = ioc.getBean(UserController.class);
        UserService service = ioc.getBean(UserService.class);
        System.out.println(controller);
        System.out.println(service);
        System.out.println("ok");
    }

    /**
     * bean--spring-el表达式
     */
    @Test
    public void getBean20() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans04.xml");
        SpELBean bean = ioc.getBean("spELBean", SpELBean.class);
        System.out.println(bean);
    }


    /**
     * bean--自动装配
     */
    @Test
    public void getBean19() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans04.xml");
        OrderController orderController = ioc.getBean("orderController", OrderController.class);
        System.out.println(orderController.getOrderService()); //com.lcredu.service.OrderService@282003e1
        System.out.println(orderController.getOrderService().getOrderDao());//com.lcredu.dao.OrderDao@7fad8c79


    }


    /**
     * bean--从配置文件中进行赋值
     */
    @Test
    public void getBean18() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans04.xml");
        Monster monster = ioc.getBean("monster01", Monster.class);
        System.out.println(monster);


    }


    /**
     * bean--BeanPostProcessor
     */
    @Test
    public void getBean17() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans03.xml");
        House house01 = ioc.getBean("house01", House.class);
        House house02 = ioc.getBean("house02", House.class);
        //转成这个子类可以调用它的销毁方法(House destory()..)
        ((ConfigurableApplicationContext) ioc).close();
        System.out.println(house01);
        System.out.println(house02);

    }

    /**
     * bean--bean的生命周期
     */
    @Test
    public void getBean16() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        House house = ioc.getBean("house", House.class);
        //转成这个子类可以调用它的销毁方法(House destory()..)
        ((ConfigurableApplicationContext) ioc).close();
        System.out.println(house);

    }


    /**
     * bean--单例与非单例
     */
    @Test
    public void getBean15() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Person person1 = ioc.getBean("person01", Person.class);
        Person person2 = ioc.getBean("person01", Person.class);
        Person person3 = ioc.getBean("person01", Person.class);
        System.out.println(person1);
        System.out.println(person2);
        System.out.println(person3);
    }


    /**
     * bean-- 创建顺序,按配置文件顺序
     */
    @Test
    public void getBean14() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");

    }

    /**
     * bean--重利用与抽象bean
     * factory.BeanIsAbstractException
     */
    @Test
    public void getBean13() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster09 = ioc.getBean("monster09", Monster.class);
//        Monster monster10 = ioc.getBean("monster10", Monster.class);
        System.out.println(monster09);
//        System.out.println(monster10);
    }

    /**
     * bean--FactoryBean
     */
    @Test
    public void getBean12() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster = ioc.getBean("myFactoryBean", Monster.class);
        System.out.println(monster);
    }

    /**
     * bean--实例工厂
     */
    @Test
    public void getBean11() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster = ioc.getBean("my_monster02", Monster.class);
        Monster monster03 = ioc.getBean("my_monster03", Monster.class);
        System.out.println(monster);
        System.out.println(monster03);
        //同一个工厂
        System.out.println(monster == monster03);
        Monster monster04 = ioc.getBean("my_monster04", Monster.class);
        System.out.println("monster04=" + monster04);
        //不同的工厂
        System.out.println(monster03 == monster04);
    }

    /**
     * bean--静态工厂
     */
    @Test
    public void getBean10() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster = ioc.getBean("myStaticFactory", Monster.class);
        System.out.println(monster);
    }

    /**
     * bean--级联赋值
     */
    @Test
    public void getBean09() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Emp emp = ioc.getBean("emp", Emp.class);
        System.out.println(emp);
    }

    /**
     * bean--根据util:list赋值
     */
    @Test
    public void getBean08() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        BookStore bookStore = ioc.getBean("bookStore", BookStore.class);
        BookStore bookStore2 = ioc.getBean("bookStore2", BookStore.class);
        System.out.println(bookStore);
        System.out.println(bookStore2 + "2222");
        System.out.println("okk");
    }

    /**
     * bean--集合注入
     */
    @Test
    public void getBean07() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Master master = ioc.getBean("master", Master.class);
        System.out.println(master);
        System.out.println("okk");
    }

    /**
     * 根据 内部bean 实现依赖注入
     */
    @Test
    public void getBean06() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        MemberServiceImpl memberService = ioc.getBean("memberService2", MemberServiceImpl.class);
        memberService.add();
        System.out.println("okk");
    }


    /**
     * 根据 ref 实现依赖注入
     */
    @Test
    public void getBean05() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        MemberServiceImpl memberService = ioc.getBean("memberService", MemberServiceImpl.class);
        memberService.add();
    }

    /**
     * 根据 P 名称空间
     */
    @Test
    public void getBean04() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster = ioc.getBean("monster07", Monster.class);
        System.out.println(monster);
    }


    /**
     * 根据构造器(index)
     */
    @Test
    public void getBean03() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster03 = ioc.getBean("monster03", Monster.class);
        Monster monster04 = ioc.getBean("monster04", Monster.class);
        Monster monster05 = ioc.getBean("monster05", Monster.class);
        System.out.println(monster03);
        System.out.println(monster04);
        System.out.println(monster05);
    }


    @Test
    public void getBean02() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans.xml");
        Monster monster = ioc.getBean(Monster.class);
        System.out.println(monster);
    }


    @Test
    public void getBean01() {
        ApplicationContext ioc = new ClassPathXmlApplicationContext("beans02.xml");
        Person person = ioc.getBean("person01", Person.class);
        System.out.println(person);
    }
}

